﻿using Coffee_Restaurant.Database;
using System;
using System.Linq;

namespace Coffee_Restaurant.DAL
{
    class ItemDa
    {
        public int Delete(int id)
        {
            try
            {
                using (var db = new DatabaseEntities())
                {
                    var model = db.Items.FirstOrDefault(t => t.ItemId == id);
                    if (model != null)
                    {
                        db.Items.Remove(model);
                        return db.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {

                return 0;
            }
            return 0;
        }

        /// <summary>
        /// ham nhap kho
        /// </summary>
        /// <param name="id"></param>
        /// <param name="quantity"></param>
        /// <param name="sumPrice"></param>
        /// <param name="description"></param>
        /// <param name="byname"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int UpdateWarehouse(int id, int quantity, decimal sumPrice, string description, string byname, string type)
        {
            int status = 0;
            try
            {
                using (var db = new DatabaseEntities())
                {
                    var model = db.Items.FirstOrDefault(x => x.ItemId == id);
                    if (model != null)
                    {
                        switch (type)
                        {
                            case "Nhập kho":
                                if (model.InWarehouse != null)
                                {
                                    quantity = (int)(model.InWarehouse + quantity);
                                }
                                break;
                            case "Quán mua nợ":
                                if (model.InWarehouse != null)
                                {
                                    quantity = (int)(model.InWarehouse + quantity);
                                }
                                break;
                            case "Xuất kho":
                                if (model.InWarehouse != null)
                                {
                                    quantity = (int)(model.InWarehouse - quantity);
                                }
                                break;
                        }
                        model.Description = description;
                        model.InWarehouse = quantity;
                        status = db.SaveChanges();
                        if (status > 0)
                        {
                            var group = db.InOutMoneyTypes.FirstOrDefault(x => x.Name == type);
                            var groupForSearch = ""; //phan loai group cho thi chi de tien tim kiem
                            if (group != null)
                            {
                                groupForSearch = group.Group;
                            }
                            var inOutMoneyInsert = new InOutMoney()
                            {
                                Datetime = DateTime.Now,
                                ByName = byname,
                                Type = type,
                                Description = description,
                                Money = sumPrice,
                                Group = groupForSearch,
                            };
                            InOutMoneyDa.Insert(inOutMoneyInsert);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return status;
        }

        /// <summary>
        /// kiem tra hang trong kho con du khong
        /// </summary>
        /// <param name="id"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        public static bool CheckWareHouse(int id, int quantity)
        {
            using (var db = new DatabaseEntities())
            {
                var model = db.Items.FirstOrDefault(x => x.ItemId == id);
                if (model != null)
                {
                    if (model.InWarehouse < quantity)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

    }
}
